﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryMProductBySchedule : BasePage
    {
        int m_count = 0;
        public DataTable ScheduleDetailDT
        {
            set { Session["ScheduleDetailDT"] = value; }
            get
            {
                if (Session["ScheduleDetailDT"] == null)
                {
                    return CreateDt();
                }
                else
                {
                    return (DataTable)Session["ScheduleDetailDT"];
                }
            }
        }
        public DataTable CreateDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ScheduleDetailID");
            dt.Columns.Add("PlanID");
            dt.Columns.Add("WorkOrderNo");
            dt.Columns.Add("CustomerName");
            dt.Columns.Add("SendCustomerName");
            dt.Columns.Add("ProductID");
            dt.Columns.Add("ProductName");
            dt.Columns.Add("Specifications");//规格
            dt.Columns.Add("OutSide");//外径
            dt.Columns.Add("InSide");//内径
            dt.Columns.Add("Unit");//单位
            dt.Columns.Add("OrderNums");//订单数量
            dt.Columns.Add("PlanNum");//计划数量=计划单数量+损耗余量
            dt.Columns.Add("LastNum");//剩余数量=计划单数量－累计排程数量
            dt.Columns.Add("ScheduleNum");//排程数量
            dt.Columns.Add("Remark");
            dt.Columns.Add("ExpectInStockDate");//预计入库日期
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdScheduleID.Value = UIHelper.GetQueryStringValue("ScheduleID");
                BindData(0);
            }
        }


        private void BindData(int pageIndex)
        {
            string ProductID = txtProductID.Text.Trim();
            string ProductName = txtProductName.Text.Trim();
            string Specifications = txtSpecifications.Text.Trim();
            string Unit = txtUnit.Text.Trim();
            string OutSide = txtOutSide.Text.Trim();
            string InSide = txtInSide.Text.Trim();
            string ScheduleID = hdScheduleID.Value.Trim();
            DataTable dt = BsScheduleBusniess.SelectDialogList(ScheduleID, ProductID, ProductName, Specifications, Unit, OutSide, InSide, pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                DataView dv = new DataView(ScheduleDetailDT);
                dv.RowFilter = "IsDel<>'1'";
                DataTable dtTemp = dv.ToTable();

                EnableGridViewRow(grdResult, dtTemp, "WorkOrderNo", false);
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void AddPro()
        {
            ScheduleDetailDT = GetAddPro(ScheduleDetailDT).Copy();
        }

        private DataTable GetAddPro(DataTable dt)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblPlanID = gvr.FindControl("lblPlanID") as Label;
                    Label lblWorkOrderNo = gvr.FindControl("lblWorkOrderNo") as Label;
                    Label lblCustomerName = gvr.FindControl("lblCustomerName") as Label;
                    Label lblSendCustomerName = gvr.FindControl("lblSendCustomerName") as Label;
                    Label lblProductID = gvr.FindControl("lblProductID") as Label;
                    Label lblProductName = gvr.FindControl("lblProductName") as Label;
                    Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;
                    Label lblOutSide = gvr.FindControl("lblOutSide") as Label;
                    Label lblInSide = gvr.FindControl("lblInSide") as Label;
                    Label lblUnit = gvr.FindControl("lblUnit") as Label;
                    Label lblOrderNums = gvr.FindControl("lblOrderNums") as Label;
                    Label lblPlanNums = gvr.FindControl("lblPlanNums") as Label;
                    Label lblLastNum = gvr.FindControl("lblLastNum") as Label;

                    if (((CheckBox)gvr.FindControl("cbChoose")).Checked && gvr.Enabled)
                    {
                        DataRow[] drs = dtTemp.Select("WorkOrderNo='" + lblWorkOrderNo.Text.Trim() + "'  ");
                        if (drs.Length == 0)
                        {
                            DataRow dr = dtTemp.NewRow();
                            dr["PlanID"] = GetGridViewString(lblPlanID);
                            dr["WorkOrderNo"] = GetGridViewString(lblWorkOrderNo);
                            dr["CustomerName"] = GetGridViewString(lblCustomerName);
                            dr["SendCustomerName"] = GetGridViewString(lblSendCustomerName);
                            dr["ProductID"] = GetGridViewString(lblProductID);
                            dr["ProductName"] = GetGridViewString(lblProductName);
                            dr["Specifications"] = GetGridViewString(lblSpecifications);
                            dr["OutSide"] = GetGridViewString(lblOutSide);
                            dr["InSide"] = GetGridViewString(lblInSide);
                            dr["Unit"] = GetGridViewString(lblUnit);
                            dr["OrderNums"] = GetGridViewString(lblOrderNums);
                            dr["PlanNum"] = GetGridViewString(lblPlanNums);
                            dr["LastNum"] = GetGridViewString(lblLastNum);
                            dr["ScheduleNum"] = GetGridViewString(lblLastNum);
                            dr["ExpectInStockDate"] = DateTime.Now.ToString("yyyy-MM-dd");
                            dr["IsDel"] = "-1";
                            dr["IsNew"] = "1";
                            dtTemp.Rows.Add(dr);
                        }
                        else if (drs.Length > 0 && CommonMethod.GetString(drs[0]["IsDel"]) == "1")
                        {
                            drs[0]["PlanID"] = GetGridViewString(lblPlanID);
                            drs[0]["WorkOrderNo"] = GetGridViewString(lblWorkOrderNo);
                            drs[0]["CustomerName"] = GetGridViewString(lblCustomerName);
                            drs[0]["SendCustomerName"] = GetGridViewString(lblSendCustomerName);
                            drs[0]["ProductID"] = GetGridViewString(lblProductID);
                            drs[0]["ProductName"] = GetGridViewString(lblProductName);
                            drs[0]["Specifications"] = GetGridViewString(lblSpecifications);
                            drs[0]["OutSide"] = GetGridViewString(lblOutSide);
                            drs[0]["InSide"] = GetGridViewString(lblInSide);
                            drs[0]["Unit"] = GetGridViewString(lblUnit);
                            drs[0]["OrderNums"] = GetGridViewString(lblOrderNums);
                            drs[0]["PlanNum"] = GetGridViewString(lblPlanNums);
                            drs[0]["LastNum"] = GetGridViewString(lblLastNum);
                            drs[0]["IsDel"] = "-1";
                        }
                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select("WorkOrderNo='" + lblWorkOrderNo.Text.Trim() + "'  ");
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success');CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            AddPro();
            BindData(e.PageIndex);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AddPro();
            BindData(0);
        }
    }
}